package unit9;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Ex9_3_Example9_3 {
    public static void main(String[] args) throws Exception {
// 对异常进行抛出处理
        //调用例9-1中DBCon类的getConnection()创建连接对象
        Connection con = new Ex9_2_Example9_2().getConnection();
        PreparedStatement pstm = null;
        ResultSet r = null;
        int i;
        //增加一条记录
        String sql = "insert into student_inf  values (?,?,?,?)";
        pstm = con.prepareStatement(sql);
        pstm.setInt(1, 7);
        pstm.setString(2, "李明");
        pstm.setString(3, "男");
        pstm.setInt(4, 15);
        i = pstm.executeUpdate() ;
        //修改一条记录
        sql = "update student_inf set name=? where id=?";
        pstm = con.prepareStatement(sql);
        pstm.setString(1, "强强");
        pstm.setInt(2, 5);
        i = pstm.executeUpdate() ;
        //删除一条记录
        sql = "delete from  student_inf where id=?";
        pstm = con.prepareStatement(sql);
        pstm.setInt(1, 2);
        i = pstm.executeUpdate() ;
        //查询年龄为20及20以上的所有记录
        sql = "select * from  student_inf where age>=?";
        pstm = con.prepareStatement(sql);
        pstm.setInt(1, 20);
        r = pstm.executeQuery();
        while (r.next()) {
            System.out.print(r.getInt("id") + "\t");
            System.out.print(r.getString("name") + "\t");
            System.out.print(r.getString(3) + "\t");
            System.out.print(r.getInt(4) + "\n");
        }
        r.close();
        pstm.close();
        con.close();
    }
}
